<template>
	<view scroll-y="true">
		<!-- 标题栏和状态栏占位符 -->
		<view>
			<view class="nav_top">
				<view class="status-bar" :style="{height: addHight}"></view>
				<view class="topContent">
					<view class="title" :style="{color:'#fff',width:'100%'}">
						<image v-if="isInput" class='img'
							src="https://eshop-goods.obs.cn-north-4.myhuaweicloud.com/mch_logo/log.png" mode=""></image>
						<view class="name">新航向教育</view>
					</view>
					<view class="searchWrap">
						<view class='area'>
							<!-- 城市筛选 -->
							<picker
								indicator-class="picker-box"
								@change="cityChange" 
								:value="cityIndex" 
								:range="cityList" 
								range-key="title"
							>
								<view class="city_name">{{cityList[cityIndex].title}}</view>
							</picker>
							<image class="icon_s" src='../../static/triangle.png'></image>
						</view>
						<!-- 分类搜索 -->
						<view class="uni-form-item">
							<icon class='icon' type="search" size="14" />
							<input 
								v-model="searchName"
								class="searchInput" 
								type="text" 
								placeholder="搜索分类名称" 
								@input="onSearch"
								placeholder-class='placeholder' 
							/>
						</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 头部轮播 -->
		<view class="carousel-section">
			<!-- 背景色区域 -->
			<view class="titleNview-background" :style="{height:swiperHeight + 90 +'rpx'}"></view>
			<swiper class="carousel" circular :autoplay="true" :current="swiperCurrent"
				:style="{height:swiperHeight*2+'rpx'}">
				<swiper-item v-for="(item, index) in bannerList" :key="index" class="carousel-item">
					<image :src="item.imgUrl" />
				</swiper-item>
			</swiper>
		</view>
		<!-- 分类信息 -->
		<view class="work">
			<view class="title">
				<view class="title-inner">
					<text>职位分类</text>
					<!-- <text class="iconfont icon-write"></text> -->
				</view>
			</view>
			<!-- 分类列表 -->
			<view class="wrap" v-if='recommendList && recommendList.length'>
				<view 
					class='mendian-list' 
					v-for="item in recommendList" 
					:key="item.id" 
					@click="workClick(item)"
				>
					<view class="mendian-info">
						<view class="thumb">
							<image mode="widthFix" class="img" src='../../static/logo.jpg'></image>
						</view>
						<view class="content">
							<view class="title">{{item.name}}</view>
							<view class="mendian-text">分类描述：{{item.description}}</view>
						</view>
					</view>
				</view>
				<!-- 底部加载提示 -->
				<view :class="more ? 'loader' : 'noMore'">
					<view>{{loadStatus === 'loading' ? '加载中...' : loadStatus === 'more'? '加载更多'  : '没有更多了~'}}</view>
				</view>
			</view>
			<view v-else class="noData">
				暂无数据
			</view>
		</view>
	</view>
</template>

<script>
	import workItem from '@/components/workItem.vue';
	import gridItem from '@/components/gridItem.vue';
	import navTop from '@/components/navTop.vue';
	import { debounce } from '@/common/util.js';
	import {
		mapState,
		mapGetters,
		mapActions,
		mapMutations
	} from 'vuex'
	export default {
		data() {
			return {
				city_id: "", // 被选城市id
				city_name: "廊坊", // 被选城市
				bannerList: [ // 轮播图列表
					{
						id: 0,
						imgUrl: require('../../static/twe.jpg')
					},
					{
						id: 1,
						imgUrl: require('../../static/three.jpg')
					},
					{
						id: 2,
						imgUrl: require('../../static/twe.jpg')
					}
				],
				swiperCurrent: 0, // 轮播图当前分页
				recommendList: [], // 列表数据
				currentPage: 1, // 列表当前分页
				pageSize: 10, // 列表条数
				total: 0, // 列表总数量
				loadStatus: "more", // 加载状态
				searchName: '', // 分类搜索名称
				top: 0, // 手机顶部距离
				addHight: 0, // 状态栏高度
				swiperHeight: 150, // 轮播图高度
				cityIndex: 0, //选中的城市下标
				city_id: '', //在picker列表所选中项的城市id
				//城市列表数据（假数据，可以在接口函数里面发请求获取真实的后台数据）
				cityList:[
					{id: 1, title: '廊坊'},
					{id: 2, title: '北京'},
					{id: 3, title: '上海'},
					{id: 4, title: '重庆'},
					{id: 5, title: '广东'},
					{id: 6, title: '四川'},
					{id: 7, title: '天津'}
				],
				timer: null // 定时器
			}
		},
		onLoad(options) {
			const {
				searchName
			} = options;
			// 获取跳转链接中搜索参数
			this.searchName = searchName;
			// 获取token
			const token = uni.getStorageSync('user_token');
			// 判断是否是新用户
			token ? this.getCodeStr() : this.initRecommendList();
			// 获取状态栏高度
			this.getBarHeight();
			// 获取轮播图高度
			this.getElementHeight(this.bannerList);
		},
		computed: {
			...mapState(['accessToken', 'barHeight'])
		},
		components: {
			workItem,
			gridItem,
			navTop
		},
		methods: {
			...mapMutations(['setStoreAccessToken']),
			// 获取token
			getCodeStr() {
				wx.login({
					success: res => {
						let data = {
							code: res.code,
						}
						this.$api.get_ready_auth(data).then(res2 => {
							if (res2.code == 200) {
								uni.setStorageSync('user_token', res2.data.accessToken);
								this.setStoreAccessToken(res2.data.accessToken);
								this.initRecommendList();
							}
						})
					}
				})
			},
			// 城市筛选
			cityChange(e){
				this.cityIndex = e.detail.value
				// this.getCaseList()
			},
			// 获取状态栏高度
			getBarHeight() {
				// 获取状态栏高度
				if (this.barHeight.statusBarHeight) {
					this.addHight = this.barHeight.statusBarHeight + 'rpx'
				} else {
					this.isBarHeight().then((isTemp) => {
						this.$store.state.barHeight = isTemp
						this.addHight = isTemp.statusBarHeight + 'px'
					})
				}
			},
			// 动态获取banner高度
			getElementHeight(list) {
				let that = this;
				uni.getImageInfo({
					src: list[0].imgUrl,
					success: function(image) {
						uni.getSystemInfo({
							success: function(res) {
								that.swiperHeight = res.windowWidth / image.width * image.height;
							}
						});
					}
				});
			},
			// 获取推荐列表
			initRecommendList() {
				this.$api.getPosiCategray({
					page: this.currentPage,
					name: this.searchName ? this.searchName : '',
					pageSize: this.pageSize
				}).then(res => {
					if (res.code == 200) {
						if (res.data) {
							this.total = res.data.totalCount;
						}
						if (this.currentPage == 1) {
							this.recommendList = res.data.rows;
							if (this.recommendList.length < this.total) {
								this.loadStatus = "more";
							} else {
								this.loadStatus = "no-more";
							}
						} else {
							this.loadStatus = "loading";
							setTimeout(() => {
								this.recommendList = this.recommendList.concat(res.data.rows);
								if (this.recommendList.length < this.total) {
									this.loadStatus = "more";
								} else {
									this.loadStatus = "no-more";
								}
							}, 1000)
						}

						uni.hideNavigationBarLoading();
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						});
					}
				});
			},
			// 推荐-查看详情
			goDetail(item) {
				uni.navigateTo({
					// 保留当前页面，跳转到应用内的某个页面,使用uni.navigateBack可以返回到原页面
					url: "/pages/workDetail/workDetail?id=" + item.id + "&from=index",
				})
			},
			// 搜索分类
			onSearch(e) {
				// 需要防抖处理的函数
				if(this.timer) {
					clearTimeout(this.timer);
				}
				this.timer = setTimeout(()=>{
					this.searchName = e.target.value;
					this.currentPage = 1;
					this.initRecommendList();
					this.timer = null
				},1000)
			},
			// 是否登录过
			getLogin() {
				let that = this;
				wx.login({
					success: res => {
						this.code = res.code;
						if (this.code) {
							let params = {
								code: this.code
							}
							this.$api.get_ready_auth(params).then(res => {
								if (res.code === 200) {
									uni.removeStorageSync('user_token');
									uni.setStorageSync('user_token', res.data.accessToken);
									uni.navigateTo({
										url: "/pages/position/index?id=" + item.id
									})
								} else {
									uni.showModal({
										title: '温馨提示',
										content: '请先授权登录',
										confirmText: '同意',
										cancelText: '关闭',
										success: function(res) {
											if (res.confirm) {
												uni.navigateTo({
													// 保留当前页面，跳转到应用内的某个页面,使用uni.navigateBack可以返回到原页面
													url: "/pages/auth/auth"
												})
											} else if (res.cancel) {
												console.log('关闭');
											}
										}
									});
								}
							})
						}
					}
				})
			},
			// 跳转到职位列表页
			workClick(item) {
				const token = uni.getStorageSync('user_token');
				if (!token) {
					this.getLogin();
				} else {
					uni.navigateTo({
						url: "/pages/position/index?id=" + item.id
					})
				}
			}
		},

		// 上拉加载
		onReachBottom() {
			this.currentPage++;
			if (this.recommendList.length < this.total) {
				this.initRecommendList();
			} else {
				uni.hideNavigationBarLoading();
				this.loadStatus = "no-more";
			}
		},

		// 下拉刷新
		onPullDownRefresh() {
			this.loadStatus = "more";
			this.searchName = '';
			this.currentPage = 1;
			this.pageSize = 10;
			this.total = 0;
			this.initRecommendList();
			setTimeout(function() {
				uni.stopPullDownRefresh(); //停止下拉刷新动画
			}, 1000);
		},
	}
</script>

<style lang="scss" scoped>
	page {
		// background: $uni-bg-color-grey;
		background: '#fff';
	}

	.nav_top {
		width: 100vw;
		position: fixed;
		top: 0;
		left: 0;
		z-index: 900;
		display: flex;
		flex-direction: column;
		background-color: var(--primary);
		padding: 0 20upx;

		&.bgColor {
			background-color: var(--primary);
		}

		.topContent {
			display: flex;
			flex-direction: column;
			align-items: flex-start;
			width: 100vw;
			height: auto;
			position: relative;

			.title {
				display: flex;
				align-items: center;
				text-align: left;
				// margin-left: 30upx;
				// width: 200upx;
				height: 100%;
				line-height: 80upx;
				font-size: 32upx;
				font-family: '自定义字体';

				.img {
					width: 50upx;
					height: 50upx;
					border-radius: 50%;
				}

				.name {
					font-size: 34rpx;
					font-weight: bold;
				}
			}
			.searchWrap{
				display: flex;
				justify-content: flex-start;
				align-items: center;
				.area{
					width: 100rpx;
					position: relative;
					.city_name{
						font-size: 32upx;
						font-weight: bold;
						line-height: 40upx;
					}
				}
				.icon_s{
					position: absolute;
					top: 50%;
					right: 10rpx;
					width: 25rpx;
					height: 15rpx;
					transform: translateY(-50%);
				}
			}
			.uni-form-item {
				// flex:1;
				display: flex;
				align-items: center;
				width: 570upx;
				justify-content: flex-start;
				padding: 12upx 10upx;
				margin: 20upx 0;
				border-radius: 10rpx;
				font-size: 24upx;
				color: #999999;
				background: #fff;
				margin-left: 20rpx;
				position: relative;
				.icon {
					display: flex;
					margin-left: 3rpx;
				}

				.font {
					font-size: 28upx;
					line-height: 40upx;
					margin-left: 10upx;
				}

				.searchInput {
					// padding-top: 10rpx;
					width: 90%;
					font-size: 30upx;
					line-height: 40upx;
					font-weight: bold;
					margin-left: 10upx;
				}
			}
		}
	}

	/* 头部 轮播图 */
	.carousel-section {
		position: relative;
		padding-top: 260rpx;
		border-radius: 20rpx;
		overflow: hidden;

		.titleNview-placing {
			box-sizing: content-box;
		}

		.titleNview-background {
			position: absolute;
			top: 80upx;
			left: 0;
			width: 100%;
			height: 230upx;
			transition: .4s;
			background: var(--primary);
			border-radius: 0 0 20upx 20upx;
		}
	}

	.carousel {
		width: 100%;
		height: 280upx;
		padding: 0 20upx;
		background-color: #fff;
		box-sizing: border-box;

		// background: #fff;
		.carousel-item {
			width: 100%;
			height: 100%;
			// padding: 0 28upx;
			overflow: hidden;
			border-radius: 20upx;
		}

		image {
			width: 100%;
			height: 100%;
			border-radius: 10upx;
		}
	}

	.nav {
		width: 750rpx;
		height: 546rpx;
		// background: $uni-primary;
		background: #fff;
		position: fixed;
		top: var(--window-top);
		left: 0;
		z-index: 2;
	}

	.fixedTop {
		position: fixed !important;
	}

	.searchBg {
		width: 100%;
		display: none;
		background: #fff;
	}

	.searchBgShow {
		position: fixed !important;
		display: block;
		z-index: 100;
	}

	.top {
		width: 100%;
		height: 400rpx;
		background: #fff;
		padding-top: 400rpx;

		.title {
			width: 100%;
			height: 200rpx;
			font-size: 42rpx;
			font-weight: bold;
			text-align: center;
			margin-bottom: 50rpx;

			.logo {
				width: 200rpx;
				height: 200rpx;
			}
		}

		.search {
			display: flex;
			justify-content: flex-start;
			align-items: center;
			width: 650rpx;
			padding-left: 36rpx;
			border-radius: 50rpx;
			background: #fff;
			height: 80rpx;
			color: #AAAAAA;
			line-height: 80rpx;
			font-size: 32rpx;
			margin-left: 35rpx;
			position: sticky;
			top: 0;
			box-shadow: 0rpx 0rpx 10rpx 5rpx rgba(0, 0, 0, 0.3);

			.icon-search {
				margin-right: 20rpx;
			}
		}

		.desc {
			font-size: 28rpx;
			text-align: center;
			line-height: 70rpx;
			color: #ccc;
		}

		.sticky-fixed {
			position: fixed;
			top: 0;
			z-index: 999;
		}
	}


	.wrap {
		// padding-top: 696rpx;
		display: flex;
		justify-content: space-between;
		flex-wrap: wrap;

		.itemContent {
			width: 330rpx;
			height: 300rpx;
			margin-bottom: 20rpx;
			// background-image: url('../../static/bg2.png');
			// background-size: 100%;
		}
	}

	.scroll-x {
		width: 1500rpx;
		display: flex;
		align-items: center;
		height: 214rpx;
		flex-wrap: nowrap;
		flex-direction: row;
		background: $uni-bg-color;
	}

	.home-near {
		background: $uni-bg-color;
		height: 180rpx;
		margin-bottom: 10rpx;

		.nearby {
			width: 670rpx;
			margin: auto;
			background: #F6F7FB;
			padding-top: 30rpx;
			border-radius: 20rpx;
			padding-bottom: 20rpx;

			text {
				font-weight: bold;
				margin-left: 30rpx;
			}

			.near {
				display: flex;
				justify-content: space-around;
				margin-top: 18rpx;

				.item {
					width: 190rpx;
					background: $uni-bg-color;
					border-radius: 20rpx;
					height: 56rpx;
					line-height: 56rpx;
					text-align: center;
					font-size: 26rpx;
					color: $uni-text-color-grey;
				}
			}
		}
	}

	.work {
		width: 680rpx;
		margin: auto;
		background: #fff;

		.title {
			// margin-top: 20rpx;
			// background: $uni-bg-color;

			.title-inner {
				width: 680rpx;
				height: 120rpx;
				margin: auto;
				display: flex;
				justify-content: space-between;
				align-items: center;
				border-bottom: 1px solid $uni-bg-color-grey;

				text {
					font-size: 42rpx;
					font-weight: bold;
				}
			}
		}
	}

	.loader {
		background: #fff;
		padding: 30rpx 0;

		view {
			border: 10rpx solid #fff;
			border-top: 10rpx solid $uni-color-primary;
			border-right: 10rpx solid #91DAD3;
			border-bottom: 10rpx solid #B0EBE5;
			border-left: 10rpx solid #E2F7F0;
			border-radius: 50%;
			width: 40rpx;
			height: 40rpx;
			animation: spin .8s linear infinite;
			margin: auto;
		}
	}

	.noMore {
		color: $uni-text-color-grey;
		background: #fff;
		padding: 30rpx 0;
		font-size: 28rpx;
		text-align: center;
	}

	.mendian-list {
		width: 100%;
		height: auto;
	}

	.mendian-list .mendian-info {
		display: flex;
		padding: 8px;
		margin-top: 16rpx;
		border-radius: 10px;
		/* border-bottom: 1px #dedede dashed; */
		border: 1px solid #A9A9A9;
		/* background-color: #D7EEFC; */
	}

	.mendian-list .mendian-info:last-child {
		/* border-bottom: none; */
		margin-bottom: 3px;
	}

	.mendian-info .thumb {
		width: 140rpx;
		height: 140rpx;
		flex-shrink: 0;
		margin-right: 30rpx;
		border-radius: 20rpx;

		// background-color: blue;
		.img {
			width: 100%;
			height: 100%;
		}
	}

	.mendian-info .content {
		flex: 1;

	}

	.content .mendian-text {
		flex-grow: 1;
		flex-shrink: 1;
		display: flex;
		justify-content: space-between;
		flex-flow: column;
		margin-top: 20rpx;
	}

	.content .title {
		font-size: 36rpx;
		font-weight: 600;
		/* text-align: center; */
		color: #484848;
		margin-top: 10rpx;
	}

	@keyframes spin {
		0% {
			transform: rotate(0deg);
		}

		100% {
			transform: rotate(360deg);
		}
	}
</style>
